package com.hss01248.dialog.building;


import com.hss01248.dialog.ResourceTable;
import com.hss01248.dialog.Tool;
import com.hss01248.dialog.bottomsheet.BottomSheetHolder;
import com.hss01248.dialog.bottomsheet.RightMdBottomSheetDialog;
import com.hss01248.dialog.config.BottomSheetStyle;
import com.hss01248.dialog.config.ConfigBean;
import com.hss01248.dialog.config.DefaultConfig;
import com.hss01248.dialog.ios.IosActionSheetHolder;
import com.hss01248.dialog.ios.IosAlertDialogHolder;
import com.hss01248.dialog.ios.IosCenterItemHolder;
import com.hss01248.dialog.material.MaterialDialogHolder;
import com.hss01248.dialog.view.AdXHolder;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.FrameAnimationElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Window;


/**
 * Created by Administrator on 2016/10/9.
 */
public  class DialogViewBuilder {

    protected static int singleChosen;
    protected  ConfigBean buildByType(ConfigBean bean){

        Tool.fixContext(bean);

        switch (bean.type){
            case DefaultConfig.TYPE_MD_LOADING:
                Tool.newCustomDialog(bean);
                buildMdLoading(bean);
                break;
            case DefaultConfig.TYPE_PROGRESS:
                buildProgress(bean);
                break;
            case DefaultConfig.TYPE_MD_ALERT:
            case DefaultConfig.TYPE_MD_INPUT:
                if((bean.context instanceof AbilitySlice || bean.context instanceof Ability) && !bean.showAsActivity){
                    buildMdAlert(bean);
                }else {
                    buildMyMd(bean);
                }

                break;
            case DefaultConfig.TYPE_MD_SINGLE_CHOOSE:
                if((bean.context instanceof AbilitySlice || bean.context instanceof Ability) && !bean.showAsActivity){
                    buildMdSingleChoose(bean);
                }else {
                    buildMyMd(bean);
                }

                break;
            case DefaultConfig.TYPE_MD_MULTI_CHOOSE:
                if((bean.context instanceof AbilitySlice || bean.context instanceof Ability) && !bean.showAsActivity){
                    buildMdMultiChoose(bean);
                }else {
                    buildMyMd(bean);
                }

                break;
            case DefaultConfig.TYPE_IOS_HORIZONTAL:
                Tool.newCustomDialog(bean);
                buildIosAlert(bean);
                break;
            case DefaultConfig.TYPE_IOS_VERTICAL:
                Tool.newCustomDialog(bean);
                buildIosAlertVertical(bean);
                break;
            case DefaultConfig.TYPE_IOS_BOTTOM:
                Tool.newCustomDialog(bean);
                buildBottomItemDialog(bean);
                break;
            case DefaultConfig.TYPE_IOS_INPUT:
                Tool.newCustomDialog(bean);
                buildNormalInput(bean);
                break;
            case DefaultConfig.TYPE_IOS_CENTER_LIST:
                Tool.newCustomDialog(bean);
                buildIosSingleChoose(bean);
                break;
            case DefaultConfig.TYPE_CUSTOM_VIEW:
                Tool.newCustomDialog(bean);
                Component rootView = getCustomRootView(bean);
                bean.dialog.setContentCustomComponent(rootView);//setContentView(rootView);



                break;
            case DefaultConfig.TYPE_BOTTOM_SHEET_CUSTOM:
                buildBottomSheet(bean);


                break;
            case DefaultConfig.TYPE_BOTTOM_SHEET_LIST:
                buildBottomSheetLv(bean);


                break;
            case DefaultConfig.TYPE_BOTTOM_SHEET_GRID:
                buildBottomSheetLv(bean);

                break;

            case DefaultConfig.TYPE_IOS_LOADING:
                Tool.newCustomDialog(bean);
                buildLoading(bean);
                break;
            default:
                break;


        }

        return bean;
    }

    private Component getCustomRootView(ConfigBean bean) {

        if(bean.customContentHolder!=null){
            Tool.removeFromParent(bean.customContentHolder.rootView);
            if(bean.asAdXStyle){
                AdXHolder adXHolder = new AdXHolder(bean.context);
                adXHolder.assingDatasAndEvents(bean.context,bean);
                return adXHolder.rootView;
            }
            return bean.customContentHolder.rootView;
        }else {
            Tool.removeFromParent(bean.customView);
            if(bean.asAdXStyle){
                AdXHolder adXHolder = new AdXHolder(bean.context);
                adXHolder.assingDatasAndEvents(bean.context,bean);
                return adXHolder.rootView;
            }
            return bean.customView;

        }
    }

    private void buildMyMd(ConfigBean bean) {
        Tool.newCustomDialog(bean);
        MaterialDialogHolder holder = new MaterialDialogHolder(bean.context);
        bean.viewHolder = holder;
        holder.assingDatasAndEvents(bean.context,bean);
        bean.dialog.setContentCustomComponent(holder.rootView);//setContentView(holder.rootView);
    }


    private void buildProgress(ConfigBean bean) {
    }



    private void buildBottomSheetLv(final ConfigBean bean) {
        CommonDialog dialog = null;
        if(bean.hasBehaviour){
            dialog = new RightMdBottomSheetDialog(bean.context);
            bean.forceWidthPercent = 1.0f;
        }else {
            Tool.newCustomDialog(bean);
            dialog = bean.dialog;
            bean.gravity = LayoutAlignment.BOTTOM | LayoutAlignment.HORIZONTAL_CENTER;
            bean.forceWidthPercent = 1.0f;
            bean.bgRes = ResourceTable.Color_dialogutil_bg_white;
        }
        bean.dialog = dialog;


        if(bean.bottomSheetStyle ==null){
            bean.bottomSheetStyle =  BottomSheetStyle.newBuilder().build();
        }

        BottomSheetHolder bottomSheetHolder = new BottomSheetHolder(bean.context);
        bottomSheetHolder.assingDatasAndEvents(bean.context,bean);
        bean.viewHolder = bottomSheetHolder;

        dialog.setContentCustomComponent(bottomSheetHolder.rootView);//setContentView(bottomSheetHolder.rootView);


    }

    private void buildBottomSheet(ConfigBean bean) {
        final RightMdBottomSheetDialog dialog = new RightMdBottomSheetDialog(bean.context);
        Tool.removeFromParent(bean.customView);
        dialog.setContentCustomComponent(bean.customView);
        bean.forceWidthPercent = 1.0f;
        dialog.siteRemovable(bean.cancelable);
        dialog.setAutoClosable(bean.outsideTouchable);
        bean.dialog = dialog;
    }

    protected  ConfigBean buildLoading(ConfigBean bean){
        LayoutScatter scatter = LayoutScatter.getInstance(bean.context);
        Component root = scatter.parse(ResourceTable.Layout_loading, null, false);
        Image gifMovieView = (Image) root.findComponentById(ResourceTable.Id_iv_loading);

        FrameAnimationElement frameAnimationElement = new FrameAnimationElement(bean.context, ResourceTable.Graphic_ios_loading_center_gif);
        gifMovieView.setBackground(frameAnimationElement);
        frameAnimationElement.start();

        Text tvMsg = (Text) root.findComponentById(ResourceTable.Id_loading_msg);
        tvMsg.setText(bean.msg.toString());
        bean.dialog.setContentCustomComponent(root);
        return bean;
    }


    protected  ConfigBean buildMdLoading(ConfigBean bean){
        LayoutScatter scatter = LayoutScatter.getInstance(bean.context);
        Component root = scatter.parse(ResourceTable.Layout_progressview_wrapconent, null, false);
        Text tvMsg = (Text) root.findComponentById(ResourceTable.Id_loading_msg);
        tvMsg.setText(bean.msg.toString());
        bean.dialog.setContentCustomComponent(root);
        return bean;
    }

    protected  ConfigBean buildMdAlert(final ConfigBean bean){

        return bean;
    }

    protected  ConfigBean buildMdSingleChoose(final ConfigBean bean){

        return bean;
    }



    protected  ConfigBean buildMdMultiChoose(final ConfigBean bean){

        return bean;
    }

    protected  ConfigBean buildIosAlert(ConfigBean bean){
        bean.isVertical = false;
        bean.hint1 = "";
        bean.hint2 = "";
        buildIosCommon(bean);
        return bean;
    }

    protected  ConfigBean buildIosAlertVertical(ConfigBean bean){
        bean.isVertical = true;
        bean.hint1 = "";
        bean.hint2 = "";
        buildIosCommon(bean);
        return bean;
    }

    protected  ConfigBean buildIosSingleChoose(ConfigBean bean){
        IosCenterItemHolder holder = new IosCenterItemHolder(bean.context);
        bean.viewHolder = holder;

        bean.dialog.setContentCustomComponent(holder.rootView);
        holder.assingDatasAndEvents(bean.context,bean);

        bean.viewHeight = Tool.mesureHeight(holder.rootView,holder.lv);

        Window window = bean.dialog.getWindow();
        window.setLayoutAlignment(LayoutAlignment.CENTER);
        return bean;
    }

    protected  ConfigBean buildBottomItemDialog(ConfigBean bean){
        IosActionSheetHolder holder = new IosActionSheetHolder(bean.context);
        bean.viewHolder = holder;
        bean.dialog.setContentCustomComponent(holder.rootView);

        holder.assingDatasAndEvents(bean.context,bean);

        bean.viewHeight = Tool.mesureHeight(holder.rootView,holder.lv);

        Window window = bean.dialog.getWindow();
        window.setLayoutAlignment(LayoutAlignment.BOTTOM);
        return bean;
    }


    protected  ConfigBean buildNormalInput(ConfigBean bean){
        buildIosCommon(bean);
        return bean;
    }

    private ConfigBean buildIosCommon(ConfigBean bean){

        IosAlertDialogHolder holder = new IosAlertDialogHolder(bean.context);
        bean.viewHolder = holder;
        bean.dialog.setContentCustomComponent(holder.rootView);
        holder.assingDatasAndEvents(bean.context,bean);

        int height = Tool.mesureHeight(holder.rootView,holder.tvMsg,holder.et1,holder.et2);
        bean.viewHeight = height;


        return bean;

    }








}
